import { createRouter, createWebHashHistory } from 'vue-router';
import { asyncRouters } from './routes/index';
import { createNavigationGuards } from '@/router/navigation-guards/index.js';
import BaseLayout from '@/layout/base-layout/index.vue';
import { transformAsyncMenuData } from '@/utils/menu.js';
import { menu } from '../../public/menu';

console.log('transformAsyncMenuData', transformAsyncMenuData(asyncRouters));
export const router = createRouter({
  history: createWebHashHistory(import.meta.env.VITE_APP_ENV === 'production' ? '/station/' : ''),
  strict: true, // 严格模式
  routes: [
    {
      path: '/',
      component: BaseLayout,
      redirect: menu.length ? menu[0].children[0].menuUrl : '/home',
      children: transformAsyncMenuData(asyncRouters)
    }
  ],
  scrollBehavior: () => ({ left: 0, top: 0 })
});

// 白名单
const WHITE_NAME_LIST = ['/webUi'];
// reset router
export function resetRouter() {
  router.getRoutes().forEach((route) => {
    const { name } = route;
    if (name && !WHITE_NAME_LIST.includes(name)) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
}

// config router
// 配置路由器
export async function setupRouter(app) {
  createNavigationGuards(router);
  app.use(router);
  await router.isReady();
}
